<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="doAdd()"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" :index="Next" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="doEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="doDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                background
                layout="total,sizes,prev,pager,next,jumper"
                :total="total"
                :page-sizes="[5, 10, 20, 50]"
                :page-size="pageParams.pagesize"
                :current-page="pageParams.page"
                @size-change="hSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :title="isEdit?'编辑弹窗':'新增弹窗'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      :is-edit="isEdit"
      width="600px"
    >
      <el-form ref="formRef" label-width="100px" :model="formData" :rules="formRules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="doClose">取消</el-button>
          <el-button size="small" type="primary" @click="doSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getRoles, delRoles, addRoles, editRoles } from '@/api/settings'
export default {
  // 1.数据
  data() {
    return {
      // 1.1用户列表数据
      list: [],
      // 1.2页码的总数
      total: 0,
      // 1.3pageParams
      pageParams: {
        page: 1,
        pagesize: 5
      },
      // 1.4弹框的布尔值
      showDialog: false,
      // 1.5表单的数据
      formData: {
        name: '',
        description: ''
      },
      // 1.6判断是否为编辑
      isEdit: false,
      // 1.7表单校验
      formRules: {
        name: [{ required: true, trigger: 'blur', message: '角色名称不能为空' }],
        description: [{ required: true, trigger: 'blur', message: '角色描述不能为空' }]
      }
    }
  },
  watch: {
    showDialog(newValue, oldValue) {
      if (!newValue) {
        this.isEdit = false
        this.formData = {
          name: '',
          description: ''
        }
      }
    }
  },
  // 2.created
  created() {
    // 2.1调用获取角色列表
    this.loadRoles()
  },
  // 3.方法
  methods: {
    // 3.1获取角色
    async loadRoles() {
      // pige第几页,pageSize每页几条
      const res = await getRoles(this.pageParams)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 3.2当页码发生变化时
    hCurrentChange(page) {
      // (1)修改页码
      this.pageParams.page = page
      // (2)重发请求
      this.loadRoles()
    },
    // 3.3删除
    doDel(id) {
      this.$confirm('请问您确定要删除吗?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // (1)发请求
        await delRoles(id)
        // 如果删除的是最后一条数据,页码-1
        if (this.list.length === 1) {
          let page = this.pageParams.page
          page--
          page = Math.max(1, page)
          this.pageParams.page = page
        }
        // (2)弹窗+刷新数据
        this.$message.success('删除成功')
        this.loadRoles()
      }).catch(() => {
      })
    },
    // 3.4点击添加按钮
    doAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 3.5 点击确认按钮
    doSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.isEdit ? this.hEdit() : this.hAdd()
        }
      })
    },
    // 3.6新增按钮函数
    async hAdd() {
      await addRoles(this.formData)
      this.showDialog = false
      this.$message.success('新增成功')
      this.total++
      this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
      this.loadRoles()
    },
    // 3.7编辑按钮
    doEdit(row) {
      this.showDialog = true
      this.isEdit = true
      this.formData = { ...row }
    },
    // 3.8编辑按钮发请求
    async hEdit() {
      await editRoles(this.formData)
      this.showDialog = false
      this.$message.success('编辑成功')
      this.loadRoles()
    },
    // 3.9点击取消按钮
    doClose() {
      this.showDialog = false
    },
    // 3.10解决表格序号问题
    Next(index) {
      const page = this.pageParams.page
      const pagesize = this.pageParams.pagesize
      return index + 1 + (page - 1) * pagesize
    },
    // 3.11当每页的条数发生变化
    hSizeChange(pagesize) {
      this.pageParams.pagesize = pagesize
      this.loadRoles()
    }
  }
}
</script>

